package com.pet.web.controller.system;

import com.pet.common.core.controller.BaseController;
import com.pet.common.core.domain.AjaxResult;
import com.pet.common.core.page.TableDataInfo;
import com.pet.system.domain.PetBrand;
import com.pet.system.service.IPetBrandService;
import com.pet.web.controller.system.vo.BrandAddBody;
import com.pet.web.controller.system.vo.BrandQueryBody;
import com.pet.web.controller.system.vo.BrandUpdateBody;
import com.pet.web.controller.system.vo.CategoryBrandBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌Controller
 *
 * @author pet
 * @date 2022-10-09
 */
@Api(value = "品牌管理", tags = "品牌管理")
@RestController
@RequestMapping("/system/brand")
public class PetBrandController extends BaseController {
    @Autowired
    private IPetBrandService petBrandService;

    /**
     * 查询品牌列表
     */
    @ApiOperation("查询品牌列表")
    @PreAuthorize("@ss.hasPermi('system:brand:list')")
    @GetMapping("/list")
    public TableDataInfo list(BrandQueryBody brandQueryBody) {
        startPage();
        PetBrand petBrand = new PetBrand();
        petBrand.setCategory(brandQueryBody.getCategory());
        petBrand.setUserId(getUserId());
        List<PetBrand> list = petBrandService.selectPetBrandList(petBrand);
        return getDataTable(list);
    }

    @ApiOperation("添加商品，品牌中的类目下拉列表")
    @PreAuthorize("@ss.hasPermi('system:brand:list')")
    @PostMapping("/categoryList")
    public AjaxResult categoryList() {
        List<String> list = petBrandService.selectPetBrandCategoryList(getUserId());
        return AjaxResult.success(list);
    }

    @ApiOperation("添加商品，品牌中的类目下的品牌列表")
    @ApiImplicitParam(name = "categoryBrandBody", value = "categoryBrandBody", dataTypeClass = CategoryBrandBody.class)
    @PreAuthorize("@ss.hasPermi('system:brand:add')")
    @PostMapping("/categoryBrandList")
    public AjaxResult categoryBrandList(@RequestBody CategoryBrandBody categoryBrandBody) {
        List<String> list = petBrandService.selectPetBrandCategoryBrandList(getUserId(), categoryBrandBody.getCategory());
        return AjaxResult.success(list);
    }

    /**
     * 获取品牌详细信息
     */
    @ApiOperation("获取品牌详细信息")
    @PreAuthorize("@ss.hasPermi('system:brand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(petBrandService.selectPetBrandById(id));
    }

    /**
     * 新增品牌
     */
    @ApiOperation("新增品牌")
    @ApiImplicitParam(name = "brandAddBody", value = "brandAddBody", required = true, dataTypeClass = BrandAddBody.class)
    @PreAuthorize("@ss.hasPermi('system:brand:add')")
    @PostMapping
    public AjaxResult add(@RequestBody BrandAddBody brandAddBody) {
        try {
            petBrandService.insertPetBrand(brandAddBody.getCategory(), brandAddBody.getBrandName(), getUserId());
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 修改品牌
     */
    @ApiOperation("修改品牌")
    @ApiImplicitParam(name = "brandUpdateBody", value = "brandUpdateBody", required = true, dataTypeClass = BrandUpdateBody.class)
    @PreAuthorize("@ss.hasPermi('system:brand:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody BrandUpdateBody brandUpdateBody) {
        PetBrand petBrand = new PetBrand();
        if (null == brandUpdateBody.getId()) {
            return AjaxResult.error("修改时，ID不能为空！");
        }
        petBrand.setId(brandUpdateBody.getId());
        petBrand.setCategory(brandUpdateBody.getCategory());
        petBrand.setBrandName(brandUpdateBody.getBrandName());
        return toAjax(petBrandService.updatePetBrand(petBrand));
    }

    /**
     * 删除品牌
     */
    @ApiOperation("删除品牌")
    @PreAuthorize("@ss.hasPermi('system:brand:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int i = petBrandService.deletePetBrandById(id);
        return 1 == i ? toAjax(i) : AjaxResult.error("请先删除商品！");
    }
}
